unit bib;

interface
  function CarregaSeq(Tabela:String;Campo:String='Codigo'):Integer;
  function BuscaPrecoProd(iProduto:Integer;iPessoa:Integer=0):Double;
  function BuscaConfigOper(iProduto:Integer;iOperacao:Integer):INteger;
  function TrataTecla(Key:Char):INteger;

implementation
Uses M_Principal, sysUtils, windows,Forms, Messages;

function CarregaSeq(Tabela:String;Campo:String='Codigo'):Integer;
begin
  with dtmPrincipal.cdsAux do begin
    Close;
    CommandText:='Select IsNull(Max('+Campo+'),0)+1 Seq From ' + Tabela ;
    Open;
    Result:=FieldByName('Seq').AsInteger;
  end;
end;

function BuscaPrecoProd(iProduto:Integer;iPessoa:Integer=0):Double;
begin
  Result:=0;
  with dtmPrincipal.cdsAux do begin
    Close;
    CommandText:='Select Top 1 I.ValorUnit From Compras C, ItensCompra I';
    CommandText:=CommandText+' Where I.Produto = ' + IntToStr(iProduto);
    CommandText:=CommandText+' And C.Sequencial = I.SeqCompra';
    if iPessoa > 0 then begin
      CommandText:=CommandText+' and C.Pessoa = ' + IntToStr(iPessoa);
    end;
    CommandText:=CommandText+' Order By C.Data Desc';
    Open;
    if recordCount > 0 then begin
      Result:=FieldByName('ValorUnit').asFloat;
    end;
  end;
end;

function BuscaConfigOper(iProduto:Integer;iOperacao:Integer):INteger;
begin
  Result:=0;
  with dtmPrincipal.cdsAux do begin
    Close;
    CommandText:='Select CO.Categoria';
    CommandText:=CommandText+' From Produtos P';
    CommandText:=CommandText+' Join ConfigOper CO on P.Grupo = CO.GrupoMat';
    CommandText:=CommandText+' Where P.Codigo = '+IntToStr(iProduto);
    CommandText:=CommandText+' and CO.Operacao = '+IntToStr(iOperacao);
    Open;
    if RecordCount > 0 then begin
      Result:=FieldByName('Categoria').asInteger;
    end;
  end;
end;

function TrataTecla(Key:Char):Integer;
var
  Formulario:TForm;
begin
  Formulario:=Screen.ActiveForm;
  if Key=#13 then begin
    Key := #0;
    PostMessage(Formulario.Handle, WM_NEXTDLGCTL, 0, 0);
  end;
end;

end.
